package com.sx.first.dao;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import com.sx.first.dao.ApplystorageDAO;
import com.sx.first.model.Applystorage;

@Repository("applystorageDAO")
@Scope("prototype")
public class ApplystorageDAO {
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public List<Applystorage> findAll(int empId) {
		@SuppressWarnings("unchecked")
		List<Applystorage> list = sessionFactory.getCurrentSession().createQuery("from Applystorage as a where a.emp.empId = :empId")
						.setParameter("empId", empId).list();
//				for (Applystorage applystorage : list) {
//					Hibernate.initialize(applystorage.getEmp());
//					Hibernate.initialize(applystorage.getMat());
//					Hibernate.initialize(applystorage.getPro());
//					Hibernate.initialize(applystorage.getSto());
//					Hibernate.initialize(applystorage.getWar());
//					Hibernate.initialize(applystorage.getEmp().getPos());
//				}
				return list;
	}

	public void save(Applystorage app) {
		sessionFactory.getCurrentSession().save(app);
	}

	public void delete(Applystorage app) {
		sessionFactory.getCurrentSession().delete(app);
	}

	public void update(Applystorage app) {
		sessionFactory.getCurrentSession().update(app);
	}

	public Applystorage findById(int appId) {
		return sessionFactory.getCurrentSession().get(Applystorage.class, appId);
	}

	public List<Applystorage> findApply() {
		@SuppressWarnings("unchecked")
		List<Applystorage> list = sessionFactory.getCurrentSession().createQuery("from Applystorage as a group by a.emp.empId").list();
				for (Applystorage applystorage : list) {
					Hibernate.initialize(applystorage.getEmp());
					Hibernate.initialize(applystorage.getEmp().getDep());
				}
				return list;
	}

}